# Contributor: Simon Frankenberger <simon-alpine@fraho.eu>
# Maintainer: Simon Frankenberger <simon-alpine@fraho.eu>
pkgname=openjdk21-mandrel
pkgver=23.1.4.0
pkgrel=0
_jdkver=21
pkgdesc="GraalVM Native Image builder for OpenJDK $_jdkver"
url="https://github.com/graalvm/mandrel"
# Upstream supports only these architectures.
# aarch64: openjdk exception when building
arch="x86_64"
license="GPL-2.0-only WITH Classpath-exception-2.0"
depends="
	bash
	gcc
	musl-dev
	openjdk$_jdkver
	zlib-dev
	zlib-static
	"
makedepends="
	git
	gpg
	ninja-build
	ninja-is-really-ninja
	openjdk$_jdkver-static-libs
	python3
	py3-setuptools
	"
# see common.json, defined as "mx_version"
_mx_ver=6.46.1
source="mandrel-packaging-$pkgver-Final.tar.gz::https://github.com/graalvm/mandrel-packaging/archive/mandrel-$pkgver-Final.tar.gz
	https://github.com/graalvm/mandrel/archive/mandrel-$pkgver-Final.tar.gz
	https://github.com/graalvm/mx/archive/$_mx_ver/graalvm-mx-$_mx_ver.tar.gz
	fix-file-system-loop-exception.patch
	Example.java
	"
builddir="$srcdir/mandrel-packaging-$pkgver"
# net: fetches some dependencies in build phase
# textrels: libnative-image-*.so have TEXTRELs.
options="net textrels"

export MX_CACHE_DIR="$srcdir/.mx"
export JAVA_HOME="/usr/lib/jvm/java-$_jdkver-openjdk"

unpack() {
	default_unpack

	mv mandrel-mandrel-$pkgver-Final mandrel-$pkgver
	mv mandrel-packaging-mandrel-$pkgver-Final mandrel-packaging-$pkgver
}

prepare() {
	default_prepare

	cd "$srcdir"
	# Build scripts require the sources to be git repositories.
	local dir; for dir in mandrel-$pkgver mandrel-packaging-$pkgver; do
		cd "$srcdir/$dir"
		git init --quiet
		git add .
		git -c user.name=mandrel -c user.email=mandrel@example.com -c commit.gpgsign=false commit --quiet --message Import
		git -c tag.gpgsign=false tag "mandrel-$pkgver-Final"
	done

	mkdir -p "$srcdir/.home"
}

build() {
	java -ea build.java \
		--mx-home "$srcdir/mx-$_mx_ver" \
		--mandrel-home "$builddir/dist" \
		--mandrel-repo "$srcdir/mandrel-$pkgver" \
		--mandrel-version "$pkgver" \
		--skip-clean
}

# TODO: Run upstream's integration tests.
check() {
	mkdir -p "$srcdir/test"
	cd "$srcdir"/test

	cp ../Example.java .
	javac Example.java
	GRAALVM_HOME="$builddir/dist" "$builddir"/dist/bin/native-image Example

	ldd example
	ls -lh example
	./example
}

package() {
	local destdir="$pkgdir$JAVA_HOME"

	cd "$builddir"/dist

	# Copy only files unique to Mandrel (the rest is copied from OpenJDK).
	mkdir -p "$destdir"/lib
	cp -r lib/libnative-image-*.so \
		lib/graalvm \
		lib/jvmci \
		lib/svm \
		lib/truffle \
		"$destdir"/lib/

	mkdir -p "$destdir"/bin
	cp -d bin/native-image "$destdir"/bin/

	# Remove source JARs.
	find "$destdir" -name '*.zip' -delete
}

sha512sums="
9a3b976df66a82cef35a28acdbcbcf9326a35a6ed0f6765ff83a60dd1c4dc87185342a6a5e99fb166d060f2bc734546e8b6fbe9b33c39cceeb49ff60f3582ac2  mandrel-packaging-23.1.4.0-Final.tar.gz
124dbc3c26495afae75dc65660e30a89f6707d66d0002aa9d71c59b472db90c1199affc5e039f4c146f5764944cc2b998c99d5723787eccb654a02758331c54e  mandrel-23.1.4.0-Final.tar.gz
145e67d6d1656c3da4980d9dc833ec61ed50040cc69e35ed890d4fceec9534e80b56f7c04112edaf01ccfd21b2a3ec5ea951bd2706270afe99006178dbdcb4b5  graalvm-mx-6.46.1.tar.gz
8d98ef2316a12e58e2a7d920533fcaab30dc81ceadba66206af1af9a8f7f21af5cac41a6a80d2a4aef8c1eb3b293a518f242ec0530e5c955418bfb7da1d4f72b  fix-file-system-loop-exception.patch
601ac59e59d32cbaf91f7541bd8173b38c68cbec96a7fb74227017afc8e5dc6ce4b8be03e92dd34cf52370e5d52beb780808af57b17c79c53ae8ec64cd34a92d  Example.java
"
